package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.ui.pageslider.PageSliderAdapter;
import com.afkt.project.ui.pageslider.ViewCreateHelper;
import com.afkt.project.util.LogUtils;
import com.ohos.afkt.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;

import java.util.ArrayList;

public class ViewPager2Activity extends BaseActivity implements TabList.TabSelectedListener {

    private static final String TAG = "ViewPager2Activity";

    private DirectionalLayout barLayout;

    private DirectionalLayout bar;

    //返回按钮
    private Image viewPager2_return;

    //菜单按钮
    private Image viewPager2_menu;

    //TabList
    private TabList tabList;

    //菜单栏事件
    private CommonDialog commonDialog = new CommonDialog(this);
    private Component popWindow;
    private int nowPosition = 0;
    private boolean isLTR = true;
    private Text LTR;
    private Text RTL;
    private Text pop_horizontal;
    private Text pop_vertical;
    private Text reset;

    //PageSlider
    private PageSlider pageSlider;
    ArrayList<Component> pages = new ArrayList<>();
    ArrayList<Component> pages2 = new ArrayList<>();

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_viewpager2;
    }

    @Override
    protected void initView() {
        barLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_viewPager2_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_viewpager2_bar, null, false);
        barLayout.addComponent(bar);
        viewPager2_return = (Image) bar.findComponentById(ResourceTable.Id_viewpager2_return);

        viewPager2_menu = (Image) bar.findComponentById(ResourceTable.Id_viewpage2_menu);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_viewPager2_pageSlider);
        tabList = (TabList) findComponentById(ResourceTable.Id_viewPager2_tablist);
        for (int i = 1; i <= 4; i++) {
            TabList.Tab tab = tabList.new Tab(getContext());
            tab.setText("PAGER-" + i);
            tabList.addTab(tab);
        }
        tabList.setFixedMode(true);
        tabList.selectTabAt(0);
        tabList.addTabSelectedListener(this);

        pageSlider.setProvider(new PageSliderAdapter(initPageSliderViewData()));

        LogUtils.error(TAG, "initView");
    }


    private ArrayList<Component> initPageSliderViewData() {
        pages = new ArrayList<>();
        ViewCreateHelper viewCreateHelper = new ViewCreateHelper();
        for (int i = 1; i <= 4; i++) {
            pages.add(viewCreateHelper.createViewPager2View(this, i));
        }
        for (int i = pages.size() - 1; i >= 0; i--) {
            pages2.add(pages.get(i));
        }
        return pages;
    }


    @Override
    protected void initListener() {

        viewPager2_return.setClickedListener(component -> terminate());

        viewPager2_menu.setClickedListener(component -> {
            if (!commonDialog.isShowing()) {
                popWindows();
            }
        });

        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                nowPosition = i;
                if (tabList.getSelectedTab().getPosition() != i) {
                    tabList.selectTabAt(i);
                }
            }
        });

    }

    private void popWindows() {
        popWindow = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_common_dialog, null, false);
        commonDialog.setContentCustomComponent(popWindow);
        commonDialog.setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        commonDialog.setAlignment(LayoutAlignment.RIGHT | LayoutAlignment.TOP);
        commonDialog.setOffset(0, 0);
        commonDialog.setSwipeToDismiss(true);
        commonDialog.siteRemovable(true);
        commonDialog.setAutoClosable(true);
        commonDialog.show();
        //菜单声明
        LTR = (Text) popWindow.findComponentById(ResourceTable.Id_pop_LTR);
        RTL = (Text) popWindow.findComponentById(ResourceTable.Id_pop_RTL);
        pop_horizontal = (Text) popWindow.findComponentById(ResourceTable.Id_pop_horizontal);
        pop_vertical = (Text) popWindow.findComponentById(ResourceTable.Id_pop_vertical);
        reset = (Text) popWindow.findComponentById(ResourceTable.Id_pop_new);
        LTR.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                LogUtils.error(TAG, "LTR点击" + nowPosition);
                if (!isLTR) {
                    tabList.removeAllComponents();
                    for (int i = 1; i <= 4; i++) {
                        TabList.Tab tab = tabList.new Tab(getContext());
                        tab.setText("PAGER-" + i);
                        tabList.addTab(tab);
                    }
                    isLTR = true;
                    switch (nowPosition) {
                        case 0:
                            nowPosition = 3;
                            break;
                        case 1:
                            nowPosition = 2;
                            break;
                        case 2:
                            nowPosition = 1;
                            break;
                        case 3:
                            nowPosition = 0;
                            break;
                    }
                    tabList.selectTabAt(nowPosition);
                    pageSlider.setProvider(new PageSliderAdapter(pages));
                    pageSlider.setCurrentPage(nowPosition);
                }
                commonDialog.remove();
            }
        });

        RTL.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                LogUtils.error(TAG, "RTL点击" + nowPosition);
                if (isLTR) {
                    tabList.removeAllComponents();
                    for (int i = 4; i >= 1; i--) {
                        TabList.Tab tab = tabList.new Tab(getContext());
                        tab.setText("PAGER-" + i);
                        tabList.addTab(tab);
                    }
                    isLTR = false;
                    switch (nowPosition) {
                        case 0:
                            nowPosition = 3;
                            break;
                        case 1:
                            nowPosition = 2;
                            break;
                        case 2:
                            nowPosition = 1;
                            break;
                        case 3:
                            nowPosition = 0;
                            break;
                    }
                    tabList.selectTabAt(nowPosition);
                    pageSlider.setProvider(new PageSliderAdapter(pages2));
                    pageSlider.setCurrentPage(nowPosition);
                }
                commonDialog.remove();
            }
        });

        pop_horizontal.setClickedListener(component -> {
            pageSlider.setOrientation(Component.HORIZONTAL);
            commonDialog.remove();
        });

        pop_vertical.setClickedListener(component -> {
            pageSlider.setOrientation(Component.VERTICAL);
            commonDialog.remove();
        });

        reset.setClickedListener(component -> {
            tabList.removeAllComponents();
            for (int i = 1; i <= 4; i++) {
                TabList.Tab tab = tabList.new Tab(getContext());
                tab.setText("PAGER-" + i);
                tabList.addTab(tab);
            }
            isLTR = true;
            commonDialog.remove();
            tabList.selectTabAt(0);
            pageSlider.setProvider(new PageSliderAdapter(pages));
            pageSlider.setCurrentPage(Component.HORIZONTAL);
            pageSlider.setOrientation(Component.HORIZONTAL);
        });
    }

    @Override
    public void onSelected(TabList.Tab tab) {
        if (pageSlider.getCurrentPage() != tab.getPosition()) {
            pageSlider.setCurrentPage(tab.getPosition());
        }
    }

    @Override
    public void onUnselected(TabList.Tab tab) {

    }

    @Override
    public void onReselected(TabList.Tab tab) {

    }


    @Override
    protected void onBackground() {
        LogUtils.error(TAG, "onBackground");
        barLayout.removeComponent(bar);
        tabList.removeAllComponents();
        super.onBackground();
    }
}
